<?php

namespace addons\Attend\common\models;

use Yii;
use yii\db\Expression;


/**
 * This is the model class for table "{{%addon_oa_attend}}".
 *
 * @property int $id ID
 * @property int $member_id 用户
 * @property int $type 打卡类型
 * @property int $status 状态
 * @property int $created_at 打卡时间
 * @property int $updated_at 修改时间
 */
class AttendCheck extends \common\models\base\BaseModel
{
//    public $state = 0;
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%addon_attend_check}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['type','classes_id','covers'], 'required'],
            [['merchant_id','member_id','classes_id', 'type', 'state', 'status', 'created_at', 'updated_at'], 'integer'],
            [['lon', 'lat', 'address','begin_time','end_time','picture'], 'string'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'member_id' => '用户',
            'type' => '打卡类型',
            'picture' => '主图',
            'covers' => '打卡图片',
            'state' => '打卡状态',
            'begin' => '开始时间',
            'end' => '结束时间',
            'classes_id' => '打卡班次',
            'status' => '状态',
            'created_at' => '打卡时间',
            'updated_at' => '修改时间',
        ];
    }

    /**
     * 关联用户
     *
     * @return \yii\db\ActiveQuery
     */
    public function getMember()
    {
        return $this->hasOne(\common\models\merchant\Member::class, ['id' => 'member_id']);
    }
    public function getAttendClasses()
    {
        return $this->hasOne(AttendClasses::class, ['id' => 'classes_id']);
    }
    public function beforeSave($insert)
    {
        if (Yii::$app->id == 'backend' || Yii::$app->id == 'merchant')
            $member_id = Yii::$app->user->id;
        else
            $member_id = Yii::$app->user->identity->member_id;
        $this->member_id = $member_id;
        $this->merchant_id = Yii::$app->user->identity->merchant_id;
        // 更新系统主图
        $covers = \yii\helpers\Json::decode($this->covers);
        $this->picture = $covers[0] ?? '';
        return parent::beforeSave($insert);
    }


}